package com.qingzhou.web.service.impl.convert;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.mybatisflex.core.query.QueryWrapper;
import com.qingzhou.system.domain.SysPost;
import com.qingzhou.system.service.ISysPostService;
import com.qingzhou.web.repository.entity.HospitalDistrictEntity;
import com.qingzhou.web.service.HospitalDistrictService;
import org.noear.solon.Solon;

public class DistrictConverter implements Converter<String> {
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        WriteCellData<Object> emptyData = new WriteCellData<>("");
        emptyData.setType(CellDataTypeEnum.STRING);
        if(StrUtil.isEmpty(value)) {
            return emptyData;
        }

        return new WriteCellData<>(DistrictConverter.getConvertString(value));
    }

    public static String getConvertString(String value){
        if(StrUtil.isEmpty(value)) return "";
        HospitalDistrictService service = Solon.context().getBean(HospitalDistrictService.class);
        HospitalDistrictEntity entity = service.getMapper().selectOneByQuery(QueryWrapper.create().eq("district_code", value).select());
        if(ObjUtil.isNull(entity)) return value;
        return entity.getDistrictName();
    }
}
